<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>疫苗固有信息管理</title>
<meta name="decorator" content="default" />
<script type="text/javascript">
	$(document).ready(function() {
		$("#inputForm").validate( {
			submitHandler : function(form) {
				postOrderDetil(form);
			}
		});
		
		initVaccName('${bsVaccineBatchno.vaccineId}');
		initSpec('${bsVaccineBatchno.specOrigin}');
		changeCompanyCode('${bsVaccineBatchno.companyCode}');
		/* 选择注射器名称加载生产厂家 */
		$("#vaccineId").change(function() {
			changeCompanyCode();
		});
		$("#btnReset").click(function() {
			reset();
		});
	});
	
	function postOrderDetil(form) {//表单保存时  将此方法中的数据尽行保存
		debugger;
		if(!$("#companyName").valid() || $("#companyName").val()==""){
			$("#companyName").next('label[data=update]').text("生产企业不能为空!").show();
			return;
		}
		if(!checkBatchno()){
			return;
		}
		var vaccineName = $("#vaccineId").find("option:selected").text();
		//var companyName = $("#companyCode").find("option:selected").text();
		$("#vaccineName").val(vaccineName);
		$("#vaccNameAll").val(vaccineName);
		$("#businessName").val(vaccineName);
		//$("#companyName").val(companyName);
		layer.confirm('确定保存？', {
			btn : [ '保存', '取消' ]
		//按钮
		}, function() {
			layer.load(2);
			loading('正在提交，请稍等...');
			form.submit();
		});
	}
	
	function checkBatchno(){
		debugger;
		if($("#batchno").val() != $("#batchNoOld").val() 
				|| $("#companyName").val() != $("#companyNameOld").val() 
				|| $("#vaccineId").val() != $("#vaccineIdOld").val()){
			var url = "${ctx}/vaccinate/bsVaccineBatchno/checkBatchno";
			var flag =false;
			$.ajax({
				url :url,
				async: false,
				data :{"batchno":$("#batchno").val(),
						"vaccineId":$("#vaccineId").val(),
	                    "companyCode":$("#companyName").val()
                    },
				success : function(data){
					if(data.length>0){
						$("#batchno").next('label[data=update]').text("该批次已存在!").show();
					}else{
						$("#batchno").next('label[data=update]').text("").hide();
						flag = true;
					}
				}
			});
			return flag;
		}else{
			$("#batchno").next('label[data=update]').text("").hide();
			return true;
		}
	}
	function initVaccName(value){
		var select='<option value="">--请选择--</option>';
		data=$.parseJSON('${fns:getDictListJson("batch_syringe_type")}');  
		var html = "";
		for(var i=0;i<data.length;i++){
			select+="<option value='"+data[i].value+"'>"+data[i].label+"</option>";
		};
		$("#vaccineId").html(select);
		if(value){
			$("#vaccineId").val(value).select2();
		}
	}
	
	function initSpec(value){
		var select='<option value="">--请选择--</option>';
		data=$.parseJSON('${fns:getDictListJson("syringe_spec")}');  
		var html = "";
		for(var i=0;i<data.length;i++){
			select+="<option value='"+data[i].value+"'>"+data[i].label+"</option>";
		};
		$("#spec").html(select);
		if(value){
			$("#spec").val(value).select2();
		}
	}
	
	function changeCompanyCode(defaultValue){
		debugger;
		var str = $("#vaccineId").val();
		$("#companyCode").find("option").remove();
		$("#companyCode").select2();
		if (str == '') {
			change();
			return;
		}
		var url = "${ctx}/vaccinate/bsVaccineBatchno/getCompanyVaccId";
		var select = '<option value="">--请选择--</option>';
		$.ajax({
			url : url,
			data : { "id" : $("#vaccineId").val() },
			async: false,
			success : function(data) {
				data = $.parseJSON(data);
				for (var i = 0; i < data.length; i++) {
					select += '<option value = '+data[i].companyCode+'>' + data[i].companyName + '</option>';
				}
				$("#companyCode").html(select);
				if(defaultValue){
					$("#companyCode").val(defaultValue).select2();
				}
				change();
			}
		});
	}
	
	function reset(){
		location.reload();
	}
	function change() {
		debugger;
		$("#companyName").val($('#companyCode').val());
	}
	function closeIframe(){
		var index = parent.layer.getFrameIndex(window.name);
		if(index){
			parent.layer.close(index);
		}else{
			window.location.href = "${ctx}/vaccinate/bsVaccineBatchno/manageBatchno?batchType=2";
		}
	}
</script>

<style type="text/css">
.input-text {
	width: 296px;
	height: 26px;
	outline-color: #ccc;
	padding: 0px;
	position: absolute;
	z-index: 1;
}

.selected {
	width: 330px;
	height: 28px;
	border: 1px solid #e6e0e0;
	outline-color: #ccc;
	padding: 0px;
	border-radius: 6px;
	position: absolute;
	z-index: 0;
}
</style>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active">
			<a>修改注射器批次号</a>
		</li>
	</ul>
	<br/>
	<form:form id="inputForm" action="${ctx}/vaccinate/bsVaccineBatchno/save" 
		modelAttribute="bsVaccineBatchno" method="post" class="form-horizontal">
		<form:input type="hidden" path="id" id="id" />
		<input type="hidden" name="type" id="type" value = "1"/>
		<form:input type="hidden" path="batchType" id="batchType" />
		<form:input type="hidden" id="createOrgCode" path="createOrgCode"/>
		<form:input type="hidden" id="createOrgName" path="createOrgName"/>
		<form:input type="hidden" id="vaccineName" path="vaccineName"/>
		<form:input type="hidden" id="vaccNameAll" path="vaccNameAll"/>
		<form:input type="hidden" id="businessName" path="businessName"/>
		<input type="hidden" id="batchNoOld" name="batchNoOld" value="${bsVaccineBatchno.batchno}"/>
		<input type="hidden" id="vaccineIdOld" name="vaccineIdOld" value="${bsVaccineBatchno.vaccineId}"/>
		<input type="hidden" id="companyNameOld" name="companyNameOld" value="${bsVaccineBatchno.companyName}"/>
		<sys:message content="${message}" />
		<div class="row-fluid">
			<div class="control-group span6 minWidth" data-line="line1">
				<label class="control-label"><font color="red">*</font> 注射器类型：</label>
				<div class="controls">
					<select id="vaccineId" name="vaccineId" class="span7 required">
						<option value="">--请选择--</option>
						<c:forEach items="${fns:getDictList('batch_syringe_type')}"
							var="dict">
							<option value="${dict.value}">${dict.label}</option>
						</c:forEach>
					</select> 
				</div>
			</div>

			<div class="control-group span6 minWidth" data-line="line2">
				<label class="control-label"><font color="red">*</font> 生产企业：</label>
				<div class="controls">
					<input id="companyName" name="companyName" class="input-text" type="text"/>
					<select id="companyCode" name="companyCode" class="selected" onchange="change()"> </select>
					<label hidden="true" class="error"  data="update">必填信息</label>
				</div>
			</div>
		</div>

		<div class="row-fluid">
			<div class="control-group span6 minWidth"  data-line="line3">
				<label class="control-label"><font color="red">*</font> 规格：</label>
				<div class="controls">
					<select name="spec" id="spec" class="span7 required">
						<option value="">--请选择--</option>
					</select>
					<label hidden="true"  class="error" data="update"></label>
				</div>
			</div>

			<div class="control-group span6 minWidth"  data-line="line4">
				<label class="control-label"><font color="red">*</font> 批号：</label>
				<div class="controls">
					<input type="text" id="batchno" name="batchno" maxlength="32"
						value="${bsVaccineBatchno.batchno}" class="span7  required"
						onchange="checkBatchno()" />
					<label hidden="true"  class="error" data="update"></label>
				</div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="control-group span6 minWidth"  data-line="line5">
				<label class="control-label"><font color="red">*</font> 有效日期至：</label>
				<div class="controls">
					<input id="outBoundDate" name="outBoundDate" type="text" maxlength="20" class="span7 Wdate required"
						value="<fmt:formatDate value="${bsVaccineBatchno.outBoundDate}" pattern="yyyy-MM-dd"/>"
						onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:true});" />
				</div>
			</div>
		</div>

		<div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存" />
			<input id="btnReset" class="btn btn-primary" type="reset" value="重置" />
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)" />
			<!-- <input id="btnClose" class="btn btn-primary" type="button" onclick="closeIframe()" value="关闭" /> -->
		</div>
	</form:form>
</body>
</html>